package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.mapper.ServeBankCardMapper;
import com.jzo2o.customer.model.domain.ServeBankCard;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IServeBankCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tyj
 * @since 2025-02-09
 */
@Service
public class ServeBankCardServiceImpl extends ServiceImpl<ServeBankCardMapper, ServeBankCard> implements IServeBankCardService {

    @Override
    public void addBankCard(BankAccountUpsertReqDTO reqDTO) {
        //判断是否存在银行卡 如果存在就修改，不存在就新增
        ServeBankCard serveBankCardOne = lambdaQuery()
                .eq(ServeBankCard::getUserId, UserContext.currentUserId())
                .one();

        if (ObjectUtil.isEmpty(serveBankCardOne)){
            //新增
            ServeBankCard serveBankCard = BeanUtils.copyBean(reqDTO, ServeBankCard.class);
            serveBankCard.setUserId(UserContext.currentUserId());
            int insert = baseMapper.insert(serveBankCard);
            if (insert!=1){
                throw  new CommonException("新增银行失败");
            }
        }else {
            //修改
            ServeBankCard serveBankCard = BeanUtils.copyBean(reqDTO, ServeBankCard.class);
            serveBankCard.setId(serveBankCardOne.getId());
            int flag = baseMapper.updateById(serveBankCard);
            if (flag!=1){
                throw  new CommonException("修改银行失败");
            }
        }

    }

    @Override
    public ServeBankCard currentUserBankAccount() {
        Long aLong = UserContext.currentUserId();
        ServeBankCard serveBankCard = lambdaQuery()
                .eq(ServeBankCard::getUserId, UserContext.currentUserId())
                .one();
        return serveBankCard;
    }
}
